//
//  MsgContentView.m
//  ChangChat
//
//  Created by Cocoa on 2019/8/22.
//  Copyright © 2019 yibai. All rights reserved.
//

#import "MsgContentView.h"

#define msg_mask_cornerRaius 12

@implementation MsgContentView

- (void)layoutSubviews {
    [super layoutSubviews];
    
    self.layer.mask = [self drawMaskLayer];
    self.needUpdateMask = NO;
}
- (void)setMsgDirection:(NSInteger)msgDirection {
    if (_msgDirection != msgDirection) {
        _msgDirection = msgDirection;
        [self setNeedsLayout];
    }
}
- (CAShapeLayer *)drawMaskLayer {
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.fillColor = UIColor.k_t1Color.CGColor;
//    maskLayer.fillColor = _msgDirection == 1 ? UIColor.k_t1Color.CGColor : UIColor.k_hexColor(0xe0e0e0).CGColor;
    maskLayer.fillColor = UIColor.redColor.CGColor;
    maskLayer.lineWidth = 0.01;
    maskLayer.lineCap = kCALineCapRound;
    maskLayer.strokeColor = UIColor.greenColor.CGColor;
    UIBezierPath *path = [UIBezierPath bezierPath];
    ///  从左下角上面开始画
    
    [path moveToPoint:CGPointMake(0, self.height - msg_mask_cornerRaius)];
    
    if (_msgDirection == 1) {
        [path addLineToPoint:CGPointMake(0, msg_mask_cornerRaius)];
        [path addArcWithCenter:CGPointMake(msg_mask_cornerRaius, msg_mask_cornerRaius) radius:msg_mask_cornerRaius startAngle:-M_PI endAngle:-M_PI_2 clockwise:YES];
        [path addLineToPoint:CGPointMake(self.width, 0)];
        
    } else {
        [path addLineToPoint:CGPointMake(0, 0)];
        [path addArcWithCenter:CGPointMake(self.width-msg_mask_cornerRaius, msg_mask_cornerRaius) radius:msg_mask_cornerRaius startAngle:-M_PI_2 endAngle:0 clockwise:YES];
    }
    
    [path addLineToPoint:CGPointMake(self.width, self.height-msg_mask_cornerRaius)];
    [path addArcWithCenter:CGPointMake(self.width-msg_mask_cornerRaius, self.height-msg_mask_cornerRaius) radius:msg_mask_cornerRaius startAngle:-M_PI*2 endAngle:-M_PI-M_PI_2 clockwise:YES];
    [path addLineToPoint:CGPointMake(msg_mask_cornerRaius, self.height)];
    [path addArcWithCenter:CGPointMake(msg_mask_cornerRaius, self.height-msg_mask_cornerRaius) radius:msg_mask_cornerRaius startAngle:-M_PI-M_PI_2 endAngle:-M_PI clockwise:YES];
    
    
    [path closePath];
    maskLayer.path = path.CGPath;
    return maskLayer;
}

@end
